#include "widget.h"
#include "ui_widget.h"
#include "QLabel"
#include "sizeutil.h"
#include "QVBoxLayout"
#include "QHBoxLayout"
#include "QScreen"
#include "QPushButton"
#include "QListWidget"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    QHBoxLayout *lay1 = new QHBoxLayout;
    QLabel *label = new QLabel(this);
    QPixmap pic("assets:/bluetooth.png");
    label->setPixmap(pic);
    lay1->addWidget(label);

    QButtonGroup *btnGrp = new QButtonGroup(this);
    btnGrp->setExclusive(true);
    QPushButton *btn;
    for (int i=0; i<4; i++)
    {
        btn = new QPushButton(this);
        btn->setText(name[i]);
        btnGrp->addButton(btn, i);
        lay1->addWidget(btn);
    }

    list = new QListWidget(this);
    QSizePolicy sp = list->sizePolicy();
    sp.setHorizontalPolicy(QSizePolicy::Expanding);
    sp.setVerticalPolicy(QSizePolicy::Expanding);
    list->setSizePolicy(sp);

    QVBoxLayout *mainLay = new QVBoxLayout;
    mainLay->addLayout(lay1);
    //mainLay->addStretch();
    mainLay->addWidget(list);

    setLayout(mainLay);

    connect(btnGrp, SIGNAL(buttonClicked(int)), this, SLOT(ButtonsClicked(int)));
    connect(list, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(ListItemClicked(QListWidgetItem*)));
}

Widget::~Widget()
{
    delete ui;
}

void Widget::ButtonsClicked(int id)
{
    QString str;

    str = name[id] + " is clicked";
    //list->addItem(str);
    list->insertItem(0, str);
}

void Widget::ListItemClicked(QListWidgetItem *item)
{
    list->insertItem(0, "item(" + item->text() + ") is clicked");
}
